﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace MapControl.Misc
{
    public class BigSquareCoord : SquareCoord
    {
        public BigSquareCoord(RectangleF rect, int colNum, int rowNum)
            : base(rect, colNum, rowNum)
        {

        }

        /// <summary>
        /// Draw Big BorderSquare
        /// </summary>
        /// <param name="g"></param>
        /// <param name="width"></param>
        public override void Draw(Graphics g, float width)
        {
            Pen pen = new Pen(Color.DarkGray, 2);

            //Vẽ các trục giữa
            for (int i = 1; i < NumOfColumns; i++)
                g.DrawLine(pen, BorderSquare.X + Width * i, BorderSquare.Y, BorderSquare.X + Width * i, BorderSquare.Y + BorderSquare.Size.Height);

            //Vẽ các trục ngang
            for (int j = 1; j < NumOfRows; j++)
                g.DrawLine(pen, BorderSquare.X, BorderSquare.Y + Height * j, BorderSquare.X + BorderSquare.Size.Width, BorderSquare.Y + Height * j);

            //Vẽ chữ số
            int index = 0;

            SolidBrush myBrush = new SolidBrush(Color.Red);
            Font myFont = new Font("Times New Roman", 24);

            // Create a StringFormat object with the each line of text, and the block 
            // of text centered on the page.
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            for (int j = 0; j < NumOfRows; j++)
            {
                float y = BorderSquare.Y + Height * j;
                for (int i = 0; i < NumOfColumns; i++)
                {
                    string num = BigSquareArray[index++];
                    float x = BorderSquare.X + Width * i;
                    RectangleF rect1 = new RectangleF(x, y, Width, Height);
                    g.DrawString(num, myFont, myBrush, rect1, stringFormat);
                }
            }

            myBrush.Dispose();
            myFont.Dispose();
            pen.Dispose();
        }

        /// <summary>
        /// Xác định tọa độ big square
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public override string GetCoord(PointF p)
        {
            string value = "";
            int X, Y;
            X = (int)((p.X - BorderSquare.X) / Width);
            Y = (int)((p.Y - BorderSquare.Y) / Height);
            
            //Lưu thông tin tọa độ ô vuông hiện tại
            currentSquare = new RectangleF(BorderSquare.X + X * Width, BorderSquare.Y + Y * Height, Width, Height);

            value = BigSquareArray[Y * NumOfColumns + X];
            return value;
        }
    }
}
